package com.waa.gulimall.product.service.impl;

import com.waa.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.waa.gulimall.product.dao.AttrDao;
import com.waa.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.waa.gulimall.product.entity.AttrEntity;
import com.waa.gulimall.product.service.AttrService;
import com.waa.gulimall.product.vo.AttrGroupRelationVo;
import com.waa.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.waa.gulimall.product.vo.SkuItemVo;
import com.waa.gulimall.product.vo.SpuItemAttrGroupVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waa.gulimall.common.utils.PageUtils;
import com.waa.gulimall.common.utils.Query;

import com.waa.gulimall.product.dao.AttrGroupDao;
import com.waa.gulimall.product.entity.AttrGroupEntity;
import com.waa.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
@Slf4j
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
  @Autowired
  private AttrAttrgroupRelationDao relationDao;
  @Autowired
  private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        if (catelogId==0){
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );
        return  new PageUtils(page);
        }else {
            String key =(String) params.get("key");
             QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);
            if (!StringUtils.isEmpty(key)){
                wrapper.and((obj)->{
                   obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            final IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public void deleteRelation(AttrGroupRelationVo[] vos) {
          log.info("进入deleteRelation");
        final List<AttrAttrgroupRelationEntity> collect = Arrays.asList(vos).stream().map((item) -> {
            final AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        relationDao.deleteBatchIdsRelation(collect);
    }
    /*
    * 根据catelogId查出所有的分组，再根据分组id查出里面所有的属性
    * */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrs(Long catelogId) {
           //查询分组信息
        final List<AttrGroupEntity> attrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        final List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupEntityList.stream().map(item -> {
            final AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item,attrGroupWithAttrsVo);
            log.info(item.toString());
            log.info(attrGroupWithAttrsVo.toString());
            final List<AttrEntity> attrRelation = attrService.getAttrRelation(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrRelation);
            //            BeanUtils.copyProperties(item,attrGroupEntityList);
//            log.info(attrGroupWithAttrsVo.getAttrGroupId()+"");
//            final List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupWithAttrsVo.getAttrGroupId()));
//            final List<Long> list = relationEntities.stream().map(val -> {
//                return val.getAttrId();
//            }).collect(Collectors.toList());
//            final List<AttrEntity> attrEntityList = attrDao.selectBatchIds(list);
//            attrGroupWithAttrsVo.setAttrs(attrEntityList);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return attrGroupWithAttrsVos;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
       //查询当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        List<SpuItemAttrGroupVo> attrGroupVoList = this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return  attrGroupVoList;
    }

}